package com.ruoyi.service.jpjs.costsettlement.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.mapper.jpjs.costsettlement.CostSettlementMapper;
import com.ruoyi.service.jpjs.costsettlement.CostSettlementService;
import com.ruoyi.util.ChineseToPinyinUtil;
import com.ruoyi.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CostSettlementServiceImpl implements CostSettlementService {

    @Autowired
    private CostSettlementMapper costSettlementMapper;


    @Override
    public List<Map<String, Object>> findSettlementAll(Map<String, Object> map) {
        map.put("userId", ShiroUtils.getUserId());
        return costSettlementMapper.findSettlementAll(map);
    }

    @Override
    public List<Map<String, Object>> findSettlementSelectAll(Map<String, Object> map) {
        map.put("userId", ShiroUtils.getUserId());
        if(costSettlementMapper.findUserFill(ShiroUtils.getUserId())>0){
            map.put("userId", 1);
        }
        return costSettlementMapper.findSettlementSelectAll(map);
    }

    @Override
    public Map<String, Object> findSettlement(Integer id) {
        return costSettlementMapper.findSettlement(id);
    }

    @Override
    public int add(Map<String, Object> map) {
        map.put("submitBy", ShiroUtils.getUserId());
        return costSettlementMapper.add(map);
    }

    @Override
    public int editInfo(Map<String, Object> map) {
        return costSettlementMapper.editInfo(map);
    }

    @Override
    public int editSubmit(Map<String, Object> map) {
        return costSettlementMapper.editSubmit(map);
    }

    @Override
    public int editApproval(Map<String, Object> map) {
        return costSettlementMapper.editApproval(map);
    }

    @Override
    public int editSubmitFinal(Integer id) {
        return costSettlementMapper.editSubmitFinal(id);
    }

    @Override
    public int editFiled(Integer id) {
        return costSettlementMapper.editFiled(id);
    }

    @Override
    public int deleteInfo(Integer id) {
        return costSettlementMapper.deleteInfo(id);
    }

    @Override
    public String createNumber(Map<String, Object> map) {
        String maxNumber = costSettlementMapper.maxNumber(map);
        if (maxNumber == null) {
            maxNumber = "001";
        } else if (maxNumber.length() == 1) {
            maxNumber = "00" + maxNumber;
        } else if (maxNumber.length() == 2) {
            maxNumber = "0" + maxNumber;
        }
        String proName = ChineseToPinyinUtil.pinyinInitials(map.get("proName").toString());
        return map.get("compTitle") + "-" + proName + "-" + maxNumber;
    }

    @Override
    public List<Map<String, Object>> findSubmitUser() {
        return costSettlementMapper.findSubmitUser();
    }

    @Override
    public int findUserFill() {
        if (ShiroUtils.getUserId() == 1L) {
            return 1;
        }
        return costSettlementMapper.findUserFill(ShiroUtils.getUserId());
    }

    @Override
    public AjaxResult export(Map<String, Object> map) {
        List<Map<String, Object>> list = costSettlementMapper.findSettlementSelectAll(map);
        List<String> colList = new ArrayList<String>();
        colList.add("billNo");
        colList.add("proName");
        colList.add("compName");
        colList.add("businessManager");
        colList.add("auditContent");
        colList.add("teamInfo");
        colList.add("applyMoney");
        colList.add("submitTime");
        colList.add("approvalBy");
        colList.add("approvalTime");
        colList.add("settlementMoney");
        colList.add("finalTime");
        colList.add("finalMoney");
        colList.add("reduceMoney");
        colList.add("reduceRate");
        colList.add("reduceReason");
        List<String> colNameList = new ArrayList<String>();
        colNameList.add("编号");
        colNameList.add("项目名称");
        colNameList.add("工程公司");
        colNameList.add("商务经理");
        colNameList.add("审核内容");
        colNameList.add("分包班组信息");
        colNameList.add("申请金额");
        colNameList.add("提交日期");
        colNameList.add("审定人");
        colNameList.add("初审时间");
        colNameList.add("结算审定金额");
        colNameList.add("终审时间");
        colNameList.add("终审金额");
        colNameList.add("核减值");
        colNameList.add("核减率");
        colNameList.add("核减原因");
        return ExcelUtils.exportData(colNameList, colList, list, "成本结算");
    }

}
